package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.LhkRenzhengMapper;
import com.ruoyi.system.mapper.LhkRoomMapper;
import com.ruoyi.system.service.ILhkOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LhkReservationMapper;
import com.ruoyi.system.service.ILhkReservationService;

/**
 * 酒店预约管理Service业务层处理
 * 
 * @author fzb
 * @date 2025-05-29
 */
@Service
public class LhkReservationServiceImpl implements ILhkReservationService 
{
    @Autowired
    private LhkReservationMapper lhkReservationMapper;

    @Autowired
    private LhkRoomMapper lhkRoomMapper;

    @Autowired
    private LhkRenzhengMapper lhkRenzhengMapper;

    @Autowired
    private ILhkOrdersService lhkOrdersService;

    @Autowired
    private LhkCustomerServiceImpl lhkCustomerService;


    @Override
    public int insertinformation(Map<String, Object> lhkOrders) {
        System.out.println(lhkOrders);
        // 生成预约信息
        LhkReservation lhkReservation = new LhkReservation();
        lhkReservation.setCustomerId(Long.valueOf(lhkOrders.get("customerId").toString()));
        lhkReservation.setHotelId(Long.valueOf(lhkOrders.get("hotelId").toString()));
        lhkReservation.setManagerId(Long.valueOf(lhkOrders.get("managerId").toString()));
        lhkReservation.setTypeId(Long.valueOf(lhkOrders.get("roomTypeId").toString()));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 按照实际传参格式调整
        try {
            lhkReservation.setCheckinDate(sdf.parse(lhkOrders.get("ruzhu").toString()));
            lhkReservation.setCheckoutDate(sdf.parse(lhkOrders.get("likai").toString()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        List<Map<String, Object>> guestInfoList = (List<Map<String, Object>>) lhkOrders.get("guestInfoList");
        for (Map<String, Object> guestInfo : guestInfoList) {
            LhkCustomer lhkCustomer = new LhkCustomer();
            lhkCustomer.setName(guestInfo.get("name").toString());
            lhkCustomer.setPhone(lhkOrders.get("phone").toString());
            lhkCustomer.setHotelId(Long.valueOf(lhkOrders.get("hotelId").toString()));
            lhkCustomer.setUserId(Long.valueOf(lhkOrders.get("managerId").toString()));
            insertLhkReservation(lhkReservation, lhkCustomer);
        }
        // 生成订单信息
        LhkOrders lhkOrders1 = new LhkOrders();
        lhkOrders1.setHotelId(Long.valueOf(lhkOrders.get("hotelId").toString()));
        lhkOrders1.setOrderTime(new Date());
        lhkOrders1.setPaymentAmount(new BigDecimal(lhkOrders.get("price").toString()));
        lhkOrders1.setUserId(Long.valueOf(lhkOrders.get("customerId").toString()));
        lhkOrdersService.insertLhkOrders(lhkOrders1);
        return 1;
    }

    /**
     * 查询酒店预约管理
     * 
     * @param reservationId 酒店预约管理主键
     * @return 酒店预约管理
     */
    @Override
    public LhkReservation selectLhkReservationByReservationId(Long reservationId)
    {
        return lhkReservationMapper.selectLhkReservationByReservationId(reservationId);
    }

    /**
     * 查询酒店预约管理列表
     * 
     * @param lhkReservation 酒店预约管理
     * @return 酒店预约管理
     */
    @Override
    public List<LhkReservation> selectLhkReservationList(LhkReservation lhkReservation) {
        // 获取userId
        Long userId = lhkReservation.getCustomerId();
        if (lhkReservation.getRealName() != null) {
            // 根据姓名查询用户id
            Long l = lhkRenzhengMapper.selectUserIdByRealName(lhkReservation.getRealName());
            if (l!= null) {
                userId = l;
            }else {
                userId = 0L;
            }
            lhkReservation.setCustomerId(userId);
        }
        Long i = lhkReservation.getRoomId();
        Long roomId = null;
        if (i != null) {
            // 获取房间id
            LhkRoom lhkRoom = new LhkRoom();
            lhkRoom.setManagerId(lhkReservation.getManagerId().intValue());
            lhkRoom.setRoomNumber(i.toString());
            if (lhkRoomMapper.selectLhkRoomList(lhkRoom).size() != 0) {
                roomId = lhkRoomMapper.selectLhkRoomList(lhkRoom).get(0).getRoomId();
                lhkReservation.setRoomId(roomId);
            }
        }
        // 根据房间ID查询房间信息
        List<LhkReservation> lhkReservations = lhkReservationMapper.selectLhkReservationList(lhkReservation);
        // 遍历列表，查询房间信息
        for (LhkReservation reservation : lhkReservations) {
            // 调用房间服务获取房间信息
            // 这里可以使用Feign调用房间服务的接口
            LhkRoom lhkRoom = new LhkRoom();
            lhkRoom.setHotelId(reservation.getHotelId());
            lhkRoom.setRoomId(reservation.getRoomId());
            int managerId = reservation.getManagerId().intValue();
            lhkRoom.setManagerId(managerId);
            List<LhkRoom> lhkRooms = lhkRoomMapper.selectLhkRoomList(lhkRoom);
            System.out.println(lhkRooms);
            reservation.setLhkRoom(lhkRooms.get(0));
        }

        return lhkReservations;
    }

    /**
     * 新增酒店预约管理
     * 
     * @param lhkReservation 酒店预约管理
     * @return 结果
     */
    @Override
    public int insertLhkReservation(LhkReservation lhkReservation,LhkCustomer lhkCustomer) {
        boolean flag = false;
        Long id = -1L;
        // 获取空闲的房间
        List<LhkRoom> lhkRooms = lhkRoomMapper.selectLhkRoomByHotelId(lhkReservation.getHotelId());
        System.out.println("空闲房间数："+lhkRooms.size());
        if (lhkRooms.size() == 0) {
            System.out.println("房间已满");
            return 0;
        }else {
            // 遍历房间列表，判断房间类型
            for (LhkRoom lhkRoom : lhkRooms) {
                System.out.println(lhkRoom);
                if (lhkRoom.getRoomTypeId() == lhkReservation.getTypeId()){
                    // 判断房间是否空闲
                    id = lhkRoom.getRoomId();
                    flag = true;
                    break;
                }
            }
            if (flag) {
                lhkReservation.setRoomId(id);
            }else {
                return 0;
            }
        }
        lhkReservation.setCreateTime(DateUtils.getNowDate());
        int i = lhkReservationMapper.insertLhkReservation(lhkReservation);
        if (i > 0){
            // 修改房间状态
            LhkRoom lhkRoom = new LhkRoom();
            lhkRoom.setRoomId(id);
            lhkRoom.setStatus(1L); // 1表示已预约
            lhkRoom.setCheckinTime(lhkReservation.getCheckinDate());
            lhkRoom.setCheckoutTime(lhkReservation.getCheckoutDate());
            lhkRoomMapper.updateLhkRoom(lhkRoom);
            // 生成客户信息
            lhkCustomer.setRoomId(id);
            lhkCustomer.setCreateTime(DateUtils.getNowDate());
            lhkCustomerService.insertLhkCustomer(lhkCustomer);
        }
        return i;
    }

    /**
     * 修改酒店预约管理
     * 
     * @param lhkReservation 酒店预约管理
     * @return 结果
     */
    @Override
    public int updateLhkReservation(LhkReservation lhkReservation)
    {
        // 通过reservationId查询预约信息
        LhkReservation lhkReservation1 = lhkReservationMapper.selectLhkReservationByReservationId(lhkReservation.getReservationId());
        LhkRoom lhkRoom = lhkRoomMapper.selectLhkRoomByRoomNumber(lhkReservation.getRoomId().toString());
        if (lhkRoom != null){
            if (lhkReservation1.getRoomId() == lhkRoom.getRoomId()) {
                lhkReservation.setUpdateTime(DateUtils.getNowDate());
                lhkReservation.setRoomId(lhkRoom.getRoomId());
                // 根据房间id查询房间信息
                LhkRoom lhkRoom1 = lhkRoomMapper.selectLhkRoomByRoomId(lhkReservation.getRoomId());
                if (lhkRoom1 != null) {
                    if(lhkReservation.getStatus() == 3L) {
                        // 3表示取消
                        lhkRoom1.setCheckinTime(null);
                        lhkRoom1.setCheckoutTime(null);
                        lhkRoom1.setStatus(0L); // 0表示空闲
                        lhkRoomMapper.updateLhkRoom(lhkRoom1);
                    } else if (lhkReservation.getStatus() == 2L) {
                        // 2表示入住
                        lhkRoom1.setCheckinTime(DateUtils.getNowDate());
                        lhkRoom1.setCheckoutTime(lhkReservation.getCheckoutDate());
                        lhkRoom1.setStatus(2L); // 2表示已入住
                        lhkRoomMapper.updateLhkRoom(lhkRoom1);
                    }
                    else if (lhkReservation.getStatus() == 1L) {
                         // 1、表示已确认
                        lhkRoom1.setCheckinTime(lhkReservation.getCheckinDate());
                        lhkRoom1.setCheckoutTime(lhkReservation.getCheckoutDate());
                        lhkRoom1.setStatus(1L); // 1表示已预约
                        lhkRoomMapper.updateLhkRoom(lhkRoom1);
                    } else if (lhkReservation.getStatus() == 0L) {
                        // 0表示未确认
                        lhkRoom1.setCheckinTime(null);
                        lhkRoom1.setCheckoutTime(null);
                        lhkRoom1.setStatus(0L); // 0表示空闲
                        lhkRoomMapper.updateLhkRoom(lhkRoom1);
                    }
                }
                return lhkReservationMapper.updateLhkReservation(lhkReservation);
            }else {
                // 如果房间id不同，判断房间是否空闲
                if (lhkRoom.getStatus() != 0) {
                    return 0;
                }else {
                    lhkRoom.setStatus(2L);
                    lhkRoom.setCheckinTime(DateUtils.getNowDate());
                    lhkRoom.setCheckoutTime(lhkReservation.getCheckoutDate());
                    lhkRoomMapper.updateLhkRoom(lhkRoom);
                    lhkReservation.setUpdateTime(DateUtils.getNowDate());
                    // 将原来的房间状态改为空闲
                    LhkRoom lhkRoom1 = new LhkRoom();
                    lhkRoom1.setRoomId(lhkReservation1.getRoomId());
                    lhkRoom1.setStatus(0L);
                    lhkRoom1.setCheckinTime(null);
                    lhkRoom1.setCheckoutTime(null);
                    lhkRoomMapper.updateLhkRoom(lhkRoom1);
                    lhkReservation.setRoomId(lhkRoom.getRoomId());
                    return lhkReservationMapper.updateLhkReservation(lhkReservation);
                }
            }
        }else {
            return 0;
        }
    }

    /**
     * 批量删除酒店预约管理
     * 
     * @param reservationIds 需要删除的酒店预约管理主键
     * @return 结果
     */
    @Override
    public int deleteLhkReservationByReservationIds(Long[] reservationIds)
    {
        return lhkReservationMapper.deleteLhkReservationByReservationIds(reservationIds);
    }

    /**
     * 删除酒店预约管理信息
     * 
     * @param reservationId 酒店预约管理主键
     * @return 结果
     */
    @Override
    public int deleteLhkReservationByReservationId(Long reservationId)
    {
        return lhkReservationMapper.deleteLhkReservationByReservationId(reservationId);
    }

}
